from scrapy.spider import Spider
from scrapy.selector import Selector
from tutorial.items import LinkItem


class CookLinkSpider(Spider):
    name = "cooklink"
    linkprefix = "http://www.instructables.com"
    linkfix = '?ALLSTEPS'
    allowed_domains = ["instructables.com"]
    start_urls = [
        "http://www.instructables.com/tag/type-id/category-food/channel-bacon/",
        "http://www.instructables.com/tag/type-id/category-food/channel-bacon/?&offset=59",
        "http://www.instructables.com/tag/type-id/category-food/channel-bacon/?&offset=118",
        "http://www.instructables.com/tag/type-id/category-food/channel-bacon/?&offset=177",

    ]

    def parse(self, response):
        sel = Selector(response)
        containers = sel.xpath("//div[@class='projects']/ul/li")
        linklist = []
        for con in containers:
            linkitem = LinkItem()
            linkitem["link"] = self.linkprefix + con.xpath("./div/a/@href").extract()[0] + self.linkfix
            linklist.append(linkitem)
        return linklist
